<template>
  <div class="com-container">
    <div class="con-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0, //当前显示数据页数的索引
      timerId: null,
    };
  },
  created() {
    // console.log(this.$nextTick);
    //在组件创建完成后，进行回调函数的注册
    this.$socket.registerCallBack("stockData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "stockData",
      chartName: "stock",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成时,主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    //组件销毁时,取消监听器
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId);
    //在组件销毁后，进行回调函数的移除
    this.$socket.removeCallBack("stockData");
  },
  watch: {
    theme() {
      this.chartInstance.dispose(); //销毁当前图表
      this.initChart(); //重新初始化新主题的图表
      this.updateChart(); //更新图表
      this.screenAdapter(); //完成图表的适配
    },
  },
  computed: {
    ...mapState(["theme"]),
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, this.theme);
      const initOption = {
        title: {
          text: "| 库存和销量的分析",
          left: 20,
          top: 20,
        },
      };
      this.chartInstance.setOption(initOption);
      //监听鼠标移入事件
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      //监听鼠标移出事件
      this.chartInstance.on("mouseover", () => {
        this.startInterval();
      });
    },
    //获取数据
    getData(ret) {
      //获取服务器的数据,对this.allData进行赋值后,调用updateChart更新图表
      // const { data: ret } = await this.$http.get("/stock");
      this.allData = ret;
      // console.log(this.allData);
      this.updateChart();
      //调用定时器
      this.startInterval();
    },
    updateChart() {
      //处理图表需要的数据
      //圆环中心点坐标
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ];
      //圆环颜色渐变的起终点
      const colorArr = [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ];
      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;
      const showData = this.allData.slice(start, end);
      // console.log(showData);
      const seriesData = showData.map((item, index) => {
        return {
          type: "pie",
          // radius: [40, 50],
          center: centerArr[index],
          title: [
            {
              text: showData[index].name,
              top: "center",
              left: "center",
            },
          ],
          hoverAnimation: false, //关闭鼠标移入到饼图时的动画效果
          labelLine: {
            show: false,
          },
          // 此处设置position,则圆环数据中心将会不显示数据
          // label: {
          //   normal: {
          //     show: true,
          //     position: "center",
          //     color: colorArr[index][0],
          //   },
          // },
          data: [
            //销量
            {
              name: item.name + "\n\n" + item.sales,
              value: item.sales,
              label: {
                position: "center",
                color: colorArr[index][0],
              },
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0],
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1],
                  },
                ]),
              },
            },
            //库存
            {
              value: item.stock,
              itemStyle: {
                color: "#333843",
              },
            },
          ],
        };
      });
      const dataOption = {
        series: seriesData,
      };
      this.chartInstance.setOption(dataOption);
    },
    //当浏览器的大小发生变化时,调用该函数,完成屏幕适配
    screenAdapter() {
      //获取文字的大小
      const titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.2;
      const innerRadius = titleFontSize * 3;
      const outterRadius = innerRadius * 1.125;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.4,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.4,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.4,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.4,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.4,
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex > 1) {
          this.currentIndex = 0;
        }
        this.updateChart(); //更新currentIndex,更新图表
      }, 3000);
    },
  },
};
</script>

<style>
</style>